import { find } from '../../../../api/member'
import { orderList as agentOrderList, paymentByWarehourse } from '../../../../api/order'
import { reject, cancel, orderList as adminOrderList } from '../../../../api/orderAdmin'
const util = require('../../../../utils/util.js')

Page({
  data: {
    orderData: [],
    pageNum: 1,
    pageSize: 5,
    total:0,
    memberId: '',
    orderManagement: '消费订单',
    orderType: 1,
    optionsHidden: false
  },
  onLoad(options) {
    console.log(options, 'options', options.id == null);
    if (options == null) {
      options = {}
    }
    if (options.id != null) {
      this.data.memberId = options.id
      if (options.name) {
        this.data.orderManagement = options.name + '消费订单'
      }
      this.setData({
        memberId: this.data.memberId,
        orderManagement: this.data.orderManagement
      })
    } else {
      this.setData({
        optionsHidden: !this.data.optionsHidden,
      })
    }
    this.loadOrder()
  },
  refresh() {
    this.triggerEvent('refresh')
  },
  detailsClick(e) {
    wx: wx.navigateTo({
      url: '/pages/member/order/details?id=' + e.currentTarget.dataset.id + '&extension=false',
    })
  },
  payForHim(e) {
    var _this = this
    wx.showModal({
      title: '提示',
      content: '是否确认接单',
      success(res) {
        if (res.confirm) {
          paymentByWarehourse({ orderId: e.currentTarget.dataset.item.id }).then(res => {
            _this.data.orderData.splice(e.currentTarget.dataset.index, 1)
            _this.setData({
              orderData: _this.data.orderData
            })
          })
        }
      }
    })
  },
  loadOrder() {
    if (this.data.pageNum == 1) {
      this.data.orderData = []
    }
    let params = {}
    params.pageNum = this.data.pageNum
    params.pageSize = this.data.pageSize
    params.memberId = this.data.memberId
    adminOrderList(params).then(res => {
      res.data.data.forEach(item => {
        this.data.orderData.push(item)
      })
      this.setData({
        total:res.data.total,
        orderData: this.data.orderData
      })
    })
  },

  cancel(e) {
    wx.showModal({
      title: '您需要取消该订单嘛？',
      complete: (res) => {
        if (res.cancel) {
          wx.showToast({
            title: '已取消',
            icon: "none"
          })
        }
        if (res.confirm) {
          cancel(e.currentTarget.dataset.id).then(res => {
            // console.log(res,'取消')
            if (res.type == 'success') {
              wx.showToast({
                title: res.content,
                icon: res.type
              })
            }
          }).catch(error => {
            wx.showToast({
              title: error.content,
              icon: "none",
            })
          })
        }
      }
    })
  },
  reject(e) {
    wx.showModal({
      title: '您需要驳回该订单嘛？',
      complete: (res) => {
        if (res.cancel) {
          wx.showToast({
            title: '已取消',
            icon: "none"
          })
        }
        if (res.confirm) {
          reject(e.currentTarget.dataset.id).then(res => {
            if (res.type == 'success') {
              wx.showToast({
                title: res.content,
                icon: res.type
              })
            }
          }).catch(error => {
            wx.showToast({
              title: error.content,
              icon: "none",
            })
          })
        }
      }
    })
  },

  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    this.data.pageNum = 1
    setTimeout(() => {
      wx.hideNavigationBarLoading()
        this.loadOrder()
      wx.stopPullDownRefresh()
    }, 1000);
  },
  onReachBottom: function () {
    if (this.data.pageSize*this.data.pageNum>=this.data.total) {
      return
    }
      this.data.pageNum += 1
      this.loadOrder()
  },
})
